/*
 * Copyright 2014 trinea.cn All right reserved. This software is the
 * confidential and proprietary information of trinea.cn
 * ("Confidential Information"). You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with trinea.cn.
 */
package com.autoscrollview.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.autoscrollview.jakewharton.salvage.RecyclingPagerAdapter;
import com.autoscrollview.utils.ListUtils;
import com.iexbuy.ihg.App;

/**
 * ImagePagerAdapter
 * 
 * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2014-2-23
 */
public class ImagePagerAdapter extends RecyclingPagerAdapter {

    private Context context;
    private List<String> imageIdList;
    private List<OnClickListener> onClickListeners;
    private ImageView.ScaleType scaleType;

    private int size;
    private boolean isInfiniteLoop;

    public ImagePagerAdapter(Context context, List<String> imageIdList, List<OnClickListener> onClickListeners) {
        this.context = context;
        this.imageIdList = imageIdList;
        this.onClickListeners = onClickListeners;
        this.size = ListUtils.getSize(imageIdList);
        isInfiniteLoop = false;
    }

    public ImagePagerAdapter(Context context, List<String> imageIdList, List<OnClickListener> onClickListeners, ImageView.ScaleType scaleType) {
        this.context = context;
        this.imageIdList = imageIdList;
        this.onClickListeners = onClickListeners;
        this.size = ListUtils.getSize(imageIdList);
        isInfiniteLoop = false;
        this.scaleType = scaleType;
    }

    public ImagePagerAdapter(Context context, List<String> imageIdList) {
        this.context = context;
        this.imageIdList = imageIdList;
        this.onClickListeners = onClickListeners;
        this.size = ListUtils.getSize(imageIdList);
        isInfiniteLoop = false;
    }


    @Override
    public int getCount() {
        // Infinite loop
        return isInfiniteLoop ? Integer.MAX_VALUE : ListUtils.getSize(imageIdList);
    }

    /**
     * get really position
     * 
     * @param position
     * @return
     */
    private int getPosition(int position) {
        return isInfiniteLoop ? position % size : position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup container) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = holder.imageView = new ImageView(context);
            if (scaleType != null) {
                holder.imageView.setScaleType(scaleType);
            } else {
                holder.imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            }
            if (onClickListeners != null) {
                    holder.imageView.setOnClickListener(onClickListeners.get(position));
            }
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
            App.bitmapUtils.display(holder.imageView, imageIdList.get(position));
//        holder.imageView.setImageResource(imageIdList.get(getPosition(position)));
        return view;
    }

    private static class ViewHolder {
        ImageView imageView;

    }

    /**
     * @return the isInfiniteLoop
     */
    public boolean isInfiniteLoop() {
        return isInfiniteLoop;
    }

    /**
     * @param isInfiniteLoop
     *            the isInfiniteLoop to set
     */
    public ImagePagerAdapter setInfiniteLoop(boolean isInfiniteLoop) {
        this.isInfiniteLoop = isInfiniteLoop;
        return this;
    }
}
